package com.deyou;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.deyou.base.BaseActivity;
import com.deyou.fragment.HomePageFragment;
import com.deyou.fragment.MsgCenterFragment;
import com.deyou.fragment.PersonalCenterFragment;
import com.deyou.fragment.WishListFragment;
import com.taobao.sophix.SophixManager;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    @BindView(R.id.frame_view)
    LinearLayout frameView;
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    private FragmentManager fManager;
    private int lastSelectedPosition = 0;

    private HomePageFragment mHomePage;
    private WishListFragment mWishList;
    private MsgCenterFragment mMsgCenter;
    private PersonalCenterFragment mPersonalCenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        getSavedInstanceState(savedInstanceState);
        initView();
        initListener();
    }

    @Override
    protected boolean translucentStatusBar() {
        return false;
    }

    /**
     * 获取Activity中之前已保存的Fragment
     *
     * @param savedInstanceState
     */
    private void getSavedInstanceState(Bundle savedInstanceState) {
        fManager = getSupportFragmentManager();
        if (savedInstanceState != null) {
            mHomePage = (HomePageFragment) fManager.findFragmentByTag("mHomePage");
            mWishList = (WishListFragment) fManager.findFragmentByTag("mWishList");
            mMsgCenter = (MsgCenterFragment) fManager.findFragmentByTag("mMsgCenter");
            mPersonalCenter = (PersonalCenterFragment) fManager.findFragmentByTag("mPersonalCenter");
        }
    }

    private void initView() {
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setBarBackgroundColor(R.color.color_F3F2F2);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_home_sel, "首页").setInactiveIconResource(R.drawable.ic_home_nor).setActiveColor(getResources().getColor(R.color.color_FF832D)).setInActiveColor(getResources().getColor(R.color.color_272424)))
                .addItem(new BottomNavigationItem(R.drawable.ic_wishlist_sel, "收藏").setInactiveIconResource(R.drawable.ic_wishlist_nor).setActiveColor(getResources().getColor(R.color.color_FF832D)).setInActiveColor(getResources().getColor(R.color.color_272424)))
                .addItem(new BottomNavigationItem(R.drawable.ic_msg_sel, "消息").setInactiveIconResource(R.drawable.ic_msg_nor).setActiveColor(getResources().getColor(R.color.color_FF832D)).setInActiveColor(getResources().getColor(R.color.color_272424)))
                .addItem(new BottomNavigationItem(R.drawable.ic_personal_sel, "我的").setInactiveIconResource(R.drawable.ic_personal_nor).setActiveColor(getResources().getColor(R.color.color_FF832D)).setInActiveColor(getResources().getColor(R.color.color_272424)))
                .setFirstSelectedPosition(lastSelectedPosition)
                .initialise();
        setDefaultFragment();
    }

    private void initListener() {
        bottomNavigationBar.setTabSelectedListener(this);
    }

    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        if (null == mHomePage) {
            mHomePage = HomePageFragment.newInstance("首页", "首页");
            addFragment(mHomePage, "travelFragment");
            showFragment(mHomePage);
        } else {
            if (mHomePage.isHidden())
                showFragment(mHomePage);
        }
    }

    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = getSupportFragmentManager();
        //开启事务
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                if (null == mHomePage) {
                    mHomePage = HomePageFragment.newInstance("首页", "首页");
                    addFragment(mHomePage, "mHomePage");
                    showFragment(mHomePage);
                } else {
                    if (mHomePage.isHidden())
                        showFragment(mHomePage);
                }
                break;
            case 1:
                if (null == mWishList) {
                    mWishList = WishListFragment.newInstance("心愿单", "心愿单");
                    addFragment(mWishList, "mWishList");
                    showFragment(mWishList);
                } else {
                    if (mWishList.isHidden())
                        showFragment(mWishList);
                }
                break;
            case 2:
                if (null == mMsgCenter) {
                    mMsgCenter = MsgCenterFragment.newInstance("消息", "消息");
                    addFragment(mMsgCenter, "mMsgCenter");
                    showFragment(mMsgCenter);
                } else {
                    if (mMsgCenter.isHidden())
                        showFragment(mMsgCenter);
                }
                break;
            case 3:
                if (null == mPersonalCenter) {
                    mPersonalCenter = PersonalCenterFragment.newInstance("我的", "我的");
                    addFragment(mPersonalCenter, "mPersonalCenter");
                    showFragment(mPersonalCenter);
                } else {
                    if (mPersonalCenter.isHidden())
                        showFragment(mPersonalCenter);
                }
                break;
            default:
                break;
        }
        // 事务提交
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    long time;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - time > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出应用", Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis();
                return true;
            } else {
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 添加所有Fragment
     *
     * @param fragment
     */
    private void addFragment(Fragment fragment, String tag) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        ft.add(R.id.frame_view, fragment, tag);
        ft.commit();
    }

    /**
     * 显示对应的Fragment
     *
     * @param fragment
     */
    private void showFragment(Fragment fragment) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        if (mHomePage != null)
            ft.hide(mHomePage);
        if (mWishList != null)
            ft.hide(mWishList);
        if (mMsgCenter != null)
            ft.hide(mMsgCenter);
        if (mPersonalCenter != null)
            ft.hide(mPersonalCenter);
        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }
}
